<template>
	<form-container
		:title="formData.id ? '编辑车间信息' : '新增车间信息'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="编码：" name="workshopCode">
				<a-input v-model:value="formData.workshopCode" placeholder="请输入编码" allow-clear />
			</a-form-item>
			<a-form-item label="名称：" name="workshopName">
				<a-input v-model:value="formData.workshopName" placeholder="请输入名称" allow-clear />
			</a-form-item>
			<a-form-item label="备注：" name="workshopDesc">
				<a-input v-model:value="formData.workshopDesc" placeholder="请输入备注" allow-clear />
			</a-form-item>
			
		</a-form>
		<template #footer>
			<a-checkbox class="mr-2" v-if="!formData.id" v-model:checked="addNext">继续新增下一条</a-checkbox>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="positionForm">
	import { required } from '@/utils/formRules'
	import { getCurrentInstance } from 'vue'
	import { message } from 'ant-design-vue'
	import tool from '@/utils/tool'
    import workshopApi from '@/api/mes/basicdata/workshopApi'
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	const { proxy } = getCurrentInstance()
	// 默认是关闭状态
	let visible = ref(false)
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	// 定义机构元素
	const treeData = ref([])
	const submitLoading = ref(false)
	// 继续新增下一条
	const addNext = ref(false)

	// 打开抽屉
	const onOpen = (record, orgId) => {
		visible.value = true
		addNext.value = false
		formData.value = {
			workshopCode: formData.value.workshopCode,
			workshopName: formData.value.workshopName,
			workshopDesc: formData.value.workshopDesc,			
		}
		if (record) {
			formData.value = Object.assign({}, record)
		}
		
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		sName: [required('请输入学生名称')],
		age: [required('请输入年龄')],
	}
	const positionCategoryOptions = tool.dictList('POSITION_CATEGORY')
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			workshopApi
				.workshopAdd(formData.value, formData.value.id)
				.then(() => {
					if (addNext.value) {
						formData.value = {
							workshopCode: formData.value.workshopCode,
							workshopName: formData.value.workshopName,
							workshopDesc: formData.value.workshopDesc,
						}
					} else {
						visible.value = false
					}
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
